<template>
  <!-- 资讯分类管理 -->
  <div>
    <!-- 添加按钮 -->
    <el-button type="primary" @click="toAdd">新增</el-button>
    <!-- 表格 -->
    <el-table
      v-loading="loading"
      :data="categoryData"
      border
      style="width: 100%; margin-top: 10px"
    >
      <el-table-column
        type="index"
        :index="1"
        label="序号"
        width="100"
        align="center"
      />
      <el-table-column prop="id" label="排序号" width="100" align="center" />
      <el-table-column
        prop="name"
        label="分类名称"
        width="350"
        align="center"
      />
      <el-table-column prop="description" label="分类描述" align="center" />
      <el-table-column label="操作" align="center" width="350">
        <template slot-scope="{ row }">
          <el-button type="primary" @click="toEdit(row)">编辑</el-button>
          <el-button type="danger" @click="toDelete(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="visible">
      <el-form ref="ruleform" :model="form" :rules="rules">
        <el-form-item label="序号" :label-width="formLabelWidth" prop="no">
          <el-input v-model="form.no" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="分类名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="分类描述" :label-width="formLabelWidth">
          <el-input
            v-model="form.description"
            autocomplete="off"
            type="textarea"
            rows="5"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="toClose">取 消</el-button>
        <el-button type="primary" @click="toSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCategory,
  saveOrUpdateCategory,
  deleteCategory
} from '@/api/infoCategory'
export default {
  data() {
    return {
      // 存储栏目分类信息
      categoryData: [],
      // 模态框标题
      title: '',
      // 设置模态框是否可见
      visible: false,
      formLabelWidth: '120px',
      //   收集表单数据
      form: {},
      //   设置加载
      loading: true,
      //   表单校验规则
      rules: {
        no: [{ required: true, message: '请输入序号', trigger: 'blur' }],
        name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getCategory()
  },
  methods: {
    //   提交表单
    toSubmit() {
      this.$refs['ruleform'].validate(async(valid) => {
        let res
        if (valid) {
          // 发送保存或更新请求
          res = await saveOrUpdateCategory(this.form)
          this.$notify({
            type: 'success',
            message: res.message
          })
          this.getCategory()
          this.toClose()
        } else {
          this.$notify({
            type: 'error',
            message: res.message
          })
          return false
        }
      })
    },
    //   关闭模态框事件
    toClose() {
      //   setTimeout(() => {
      this.$refs['ruleform'].resetFields()
      //   }, 50);
      this.visible = false
    },
    //   删除栏目信息
    toDelete(id) {
      console.log(id)
      this.$confirm('此操作将删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await deleteCategory({ id })
          this.getCategory()
          this.$message({
            type: 'success',
            message: res.message
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: res.message
          })
        })
    },
    //   新增栏目信息
    toAdd() {
      this.visible = true
      this.title = '新增栏目分类'
      this.form = {}
    },
    //   编辑栏目信息
    toEdit(row) {
      this.visible = true
      this.title = '修改栏目分类'
      this.form = { ...row }
    },
    //   查询栏目分类信息
    async getCategory() {
      this.loading = true
      const res = await getCategory()
      //   console.log(res);
      try {
        if (res.status === 200) {
          this.categoryData = res.data
        }
      } catch (error) {
        this.$message('请求失败')
      }
      this.loading = false
    }
  }
}
</script>
<style scoped>
</style>
